/*
 * Copyright 2010 Michael Nitsche
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package de.mnit.basis.swt.statisch;

import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Device;
import org.eclipse.swt.graphics.RGB;

import de.mnit.basis.daten.struktur.schluessel.S_SchluesselListe;
import de.mnit.basis.daten.struktur.schluessel.SchluesselListe;

/**
 * @author Michael Nitsche
 * 18.01.2010	Erstellt
 */
public class FarbManager {

	private static S_SchluesselListe<String,Color> farben=SchluesselListe.neu();


	public static Color gFarbe(int... rgb) {
		return gFarbe(null, rgb[0], rgb[1], rgb[2]);
	}

	public static Color gFarbe(int rot, int gruen, int blau) {
		return gFarbe(null, rot, gruen, blau);
	}

	public static Color gFarbe(Device d, int rot, int gruen, int blau) {
		return iFarbe(d, rot, gruen, blau);
	}

	public static Color gFarbe(RGB rgb) {
		return gFarbe(null, rgb);
	}

	public static Color gFarbe(Device d, RGB rgb) {
		return iFarbe(d, rgb.red, rgb.green, rgb.blue);
	}

	// INTERN

	private static Color iFarbe(Device d, int rot, int gruen, int blau) {
		String schluessel=iSchluessel(d, rot, gruen, blau);
		if(farben.gSchluessel().kennt(schluessel)) {
			Color farbe=farben.gib(schluessel);
			if(farbe.isDisposed()) {
				farben.minus(schluessel);
				farbe=iNeu(schluessel, d, rot, gruen, blau);
			}
			return farbe;
		}
		else
			return iNeu(schluessel, d, rot, gruen, blau);
	}

	private static Color iNeu(String schluessel, Device d, int rot, int gruen, int blau) {
		Color farbe=new Color(d, rot, gruen, blau);
		farben.plus(schluessel, farbe);
		return farbe;
	}

	private static String iSchluessel(Device d, int rot, int gruen, int blau) {
		String dev= d==null ? null : d.toString();
		return dev+","+rot+","+gruen+","+blau;
	}

}
